// SGJ Apps Bible Input Method (BibleIM).  A collection
// of Java InputMethods enabling the easy typing of Biblical scripts.
// Copyright (C) 2007-2008 Steffen G. Jenkins  

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

// This file is based on the following source file.

/*
Brahmi Kannada Input Method
http://brahmi.sourceforge.net
Copyright (C) 2002  Harsha P Ravnikar <harsha_pr@users.sourcefourge.net>
This file is a part of Brahmi Kannada Input Method
 
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
 
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package uk.org.sgj.BibleIM;

import java.awt.AWTEvent;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.im.spi.InputMethod;
import java.awt.im.spi.InputMethodContext;
import java.util.Locale;

public class GeneralInputMethod implements InputMethod
{
    private InputMethodExec IME;
    private Locale locale;
    protected static KeyboardLayoutDisplay klDisplay;
    private static InputMethodContext inputmethodcontext;
    
    public GeneralInputMethod(Locale locale1, InputMethodExec inputMethodExec)
    {
        locale = locale1;
        IME = inputMethodExec;
    }
    
    @Override
    public void setInputMethodContext(InputMethodContext inputmethodcontext)
    {
        if ((klDisplay == null) || (GeneralInputMethod.inputmethodcontext != inputmethodcontext))
        {
            klDisplay = new KeyboardLayoutDisplay(null, IME);
        }
        IME.setInputMethodContext(inputmethodcontext);
    }
    
    public void setFont(Font f)
    {
        if (klDisplay != null)
        {
            klDisplay.changeFontForKeyboard(f);
        }
    }
    
    @Override
    public boolean setLocale(Locale locale1)
    {
        return locale1.getLanguage().equals(locale.getLanguage());
    }
    
    @Override
    public Locale getLocale()
    {
        return locale;
    }
    
    @Override
    public void setCharacterSubsets(Character.Subset asubset[])
    {
    }
    
    @Override
    public void setCompositionEnabled(boolean flag)
    {
        throw new UnsupportedOperationException();
    }
    
    @Override
    public boolean isCompositionEnabled()
    {
        return true;
    }
    
    @Override
    public void reconvert()
    {
        throw new UnsupportedOperationException("This Input Method does not support reconversion");
    }
    
    @Override
    public void dispatchEvent(AWTEvent awtevent)
    {
        if (awtevent instanceof KeyEvent)
        {
            KeyEvent keyevent = (KeyEvent)awtevent;
            if (awtevent.getID() == KeyEvent.KEY_TYPED)
            {
                IME.processKeyEvent(keyevent);
            }
        }
    }
    
    @Override
    public void notifyClientWindowChange(Rectangle rectangle)
    {
    }
    
    @Override
    public void activate()
    {
        if ((klDisplay != null) && (!klDisplay.isVisible()))
        {
            klDisplay.setVisible(true);
        }
    }
    
    @Override
    public void deactivate(boolean flag)
    {
    }
    
    @Override
    public void hideWindows()
    {
        if ((klDisplay != null) && (klDisplay.isVisible()))
        {
            klDisplay.setVisible(false);
        }
    }
    
    @Override
    public void removeNotify()
    {
    }
    
    @Override
    public void endComposition()
    {
        IME.commit();
    }
    
    @Override
    public void dispose()
    {
    }
    
    @Override
    public Object getControlObject()
    {
        return(this);
    }
}
